/**
  @file: Resources.java
  
   PURPOSE:
      This class establishes as properties all property resources bundled with
      either CIWi or Crux. The "resources" are static parameters (properties)
      that have been established for CIWi/Crux, and that we DO NOT intend to
      be exported for modification by an administrator!

      General resources for the service are also loaded.
   
   @author Ron Williams
  
   REVISION HISTORY ABSTRACT
  
   PROGRAMMER:  Ron Williams ORG: MSE
  
   Copyright 2011 Mission Solutions LLC. All rights reserved.
  
   END ABSTRACT
 */

package com.mse.utilities;

import java.lang.String;
import java.lang.StringBuilder;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

import com.mse.utilities.UtilityException;

/**
   DESCRIPTION:
      The Resources class facilitates loading  a "PropertyResourceBundle"
      to establish service and application properties that are NOT intended to
      be modified at installed locations (e.g. on site).
 */
public class Resources {

   /**
      DESCRIPTION:
         Create a Resources object, loading all Service, CIWi and Crux
         properties.  All Service properties are loaded from SVC.properties
         resource bundle. CIWi properties are loaded from "CIWi.properties"
         resource bundle, and Crux from "CRUX.properties". If only one
         application exists then just those properties are loaded. However if
         neither exists, an exception is thrown.

         The Service resource bundle must always exist, and we raise an
         exception if not found.

      @return void
      @throws UtilityException - if we fail to load any resources
    */
   public Resources () throws UtilityException {
      ciwiFailed = false;      // assume load of CIWi bundle ok
      cruxFailed = false;      // assume load of Crux bundle ok

      try {
         this.rbSvc = ResourceBundle.getBundle(bundleSvc);
      } catch (MissingResourceException mre) {
         // MUST have service bundle, error if not found
         throw new UtilityException("Failed to load service resources");
      }

      try {
         this.rbCiwi = ResourceBundle.getBundle(bundleCiwi);
      } catch (MissingResourceException mre) {
         ciwiFailed = true;
      }

      try {
         this.rbCrux = ResourceBundle.getBundle(bundleCrux);
      } catch (MissingResourceException mre) {
         cruxFailed = true;
      }

      // we assume success if we load just one application bundle. This 
      // supports deployment of one application.
      if (ciwiFailed && cruxFailed) {
         throw new UtilityException("Failed to load resources");
      }
   }


   /**
      DESCRIPTION:
         obtain all resource settings for CIWi and Crux. All values are returned
         as properties in a map. NOTE: depending upon configuration, it is
         possible for only one set of resource to be obtained (CIWi or Crux).

      @return propMap - all 'key' = 'value' pairs in the Resources as a map
    */
   public Map<String,String> getAsMap() {
      HashMap<String,String> aMap = new HashMap<String,String>();
      String                 aKey;

      Enumeration<String> svcKeys = rbSvc.getKeys();

      while (svcKeys.hasMoreElements()) {
         aKey = svcKeys.nextElement();
         aMap.put(aKey, rbSvc.getString(aKey));
      }
      if (! ciwiFailed) {
         Enumeration<String> keys = rbCiwi.getKeys();

         while (keys.hasMoreElements()) {
            aKey = keys.nextElement();
            aMap.put(aKey, rbCiwi.getString(aKey));
         }
      }

      if (! cruxFailed) {
         Enumeration<String> keys = rbCrux.getKeys();

         while (keys.hasMoreElements()) {
            aKey = keys.nextElement();
            aMap.put(aKey, rbCrux.getString(aKey));
         }
      }

      return aMap;
   }


   /**
      DESCRIPTION:
         obtain the string representation for this class. All properties are
         output.

      @return text - all 'key' = 'value' pairs in the Resources
    */
   public String toString() {
      StringBuilder sb = new StringBuilder(128);
      String        key;
      String        val;

      sb.append("\n");
      sb.append("\nResources: from bundle: " + bundleSvc);
      Enumeration<String> svcKeys = rbSvc.getKeys();

      while (svcKeys.hasMoreElements()) {
         key = svcKeys.nextElement();
         val = rbSvc.getString(key);
         sb.append("\n");
         sb.append(key);
         sb.append(" = ");
         sb.append(val);
      }

      sb.append("\n");

      if (ciwiFailed) {
         sb.append("\nResources: from bundle: " + bundleCiwi + " NOT LOADED");
      } else {
         sb.append("\nResources: from bundle: " + bundleCiwi);

         Enumeration<String> keys = rbCiwi.getKeys();

         while (keys.hasMoreElements()) {
            key = keys.nextElement();
            val = rbCiwi.getString(key);
            sb.append("\n");
            sb.append(key);
            sb.append(" = ");
            sb.append(val);
         }
      }

      if (cruxFailed) {
         sb.append("\n\nResources: from bundle: " + bundleCrux + " NOT LOADED");
      } else {
         sb.append("\n\nResources: from bundle: " + bundleCrux);

         Enumeration<String> keys = rbCrux.getKeys();

         while (keys.hasMoreElements()) {
            key = keys.nextElement();
            val = rbCrux.getString(key);
            sb.append("\n");
            sb.append(key);
            sb.append(" = ");
            sb.append(val);
         }
      }

      return sb.toString();
   }


//---------------------------------------------------------------PRIVATE
   private ResourceBundle rbSvc;     // property resrouce bundle for Service
   private ResourceBundle rbCiwi;    // property resrouce bundle for CIWi
   private ResourceBundle rbCrux;    // property resrouce bundle for Crux

   // path to Service property resource bundle
   private static String bundleSvc = "com.mse.service.SVC";

   // path to CIWi property resource bundle
   private static String bundleCiwi = "com.mse.ciwi.CIWi";

   // path to Crux property resource bundle
   private static String bundleCrux = "com.mse.crux.CRUX";

   private boolean ciwiFailed;      // indicates load of CIWi bundle failed
   private boolean cruxFailed;      // indicates load of Crux bundle failed

}

